
#ifndef   ZGW_HID_HAL_H
#define   ZGW_HID_HAL_H

#include "bt_types.h"
#include "lmp_types.h"

#define  NUM_OF_HARD_FIFO   16		// hardware fifio num
#define  NUM_OF_HID_FIFO	2
#define  SLICE_LENGTH		32

//#define POWER_ON_TIMER 	6400

#define PIN_FIFO_MAX_NUM    16

#define ADS129X_DRDY 23

#define SPP_SEND_TIME			24  // 16:10ms  24:15ms

#define SPP_ECG_LIMIT			25


// LED
#define  HID_LED_ON          0x01
#define  HID_LED_OFF		 0x02
#define  HID_LED_FLASH		 0x03
#define  HID_LED_MOTOR		 0x04

#define LED_Power              1
#define LED_Pair               0
#define WP_IIC_PIN             2
#define NOUSE				   				 3
#define LED_CHARGE       	   NOUSE
#define LED_CAPS			   NOUSE


// keyboard  macros & registers
#define KEYBOARD_COLUMNS                         18
#define KEYBOARD_ROWS                            8

#define NOKEY   0x0000


#define  HID_ADDR_BASE  0x40200000
#define  HID_KEY_REG    0x3c

#define  KEYBOARD_STATUS_OFFSET  	0xa4

#define   KEYBOARD_STATUS_GHOST   	0x02   // 1 = Invalid 0 = normal
#define   KEYBOARD_STATUS_FULL      0x04  // 1 = full 0 = last one
#define   KEYBOARD_STATUS_OVERFLOW  0x08  // 1 = Invalid 0 = normal
//  hid fifo start address  len = 16bytes
#define   KEYBOARD_FIFO_OFFSET   	0xa8


// mouse macros & registers
#define  MOUSE_X_DATA				0x90
#define  MOUSE_Y_DATA				0x94
#define  MOUSE_STATUS				0x9c

// wii macros & registers
#define  PWM_SELECT_EN				0x30
//#define  PWM_MODE_SEL				0x1c
#define  PWM_INI_LOW				0xf0
#define  PWM_INI_HIGH				0xf4
#define  PWM_TOGGLE_LOW				0xf8
#define  PWM_TOGGLE_HIGH			0xfc


// pmu registers
#define  PMU_ADDR_BASE	0x40250000
#define  PMU_MODE_SEL				PMU_ADDR_BASE+0x106
#define  PMU_CHANNEL_SEL			PMU_ADDR_BASE+0x108
#define  PMU_VOLTAGE_VAL			PMU_ADDR_BASE+0x120



#define MOS_NONE    0
#define MOS_XY      (1 << 3)
#define MOS_Z       (1 << 2)
#define MOS_BUTN    (1 << 0)





#define HID_READ_REG_UINT8(a)		(*((volatile u_int8 *)(a+HID_ADDR_BASE)))

#define HID_WRITE_REG_UINT8(a,v)  	\
	(*(volatile u_int8  *)(a+HID_ADDR_BASE) = (v))
	

#define HID_READ_REG_UINT32(a)			(*((volatile u_int32 *)(a+HID_ADDR_BASE)))
#define HID_WRITE_REG_UINT32(REG,VAL)   (*(volatile u_int32*)(REG+HID_ADDR_BASE)) = (u_int32)(VAL) 

typedef void (*t_hid_1)(void);
typedef u_int8 (*t_hid_2)(void);


typedef void (*t_hid_led)(u_int8  num, u_int8 mode, u_int16 toggle);
typedef void (*t_hid_pwm)(u_int8 onOff,u_int16  initial, u_int16  toggle);
typedef u_int16  (*t_hid_adc)(u_int8 channel);
typedef void (*t_hid_Set_KB_Lock_LED)(u_int8 flag);
typedef void (*t_hid_lm_disp_event)(u_int8 event_code,t_lm_event_info* p_event_info);

typedef void (*t_hid_spp_rxData)(u_int8 *ptr, u_int8 length);
typedef void (*t_hid_l2cap_connect_comp)(u_int16 handle);
typedef void (*t_hid_rx_ack)(u_int16 handle);
typedef void (*t_hid_link_sniff_timeout)(void *arg);
typedef void (*t_hid_handle_message)(u_int16 msg_id, void* msg_data);
typedef void (*t_hid_run_func)(u_int8 *data,u_int8 length);

typedef void (*t_bt_Dispatch_Event)(u_int8 event_code,t_lm_event_info* p_event_info);

void HID_Pin_SM(void);
void HID_Init_KB_SDP(void);

void HID_Init_RawData(void);
void HID_Init_SPPData(void);
void HID_InitSleepMode(void *dummy);
void HID_Init_HAL(void);
void HID_ISR(void);
void  HID_Set_PWM(u_int8 onOff,u_int16  initial, u_int16  toggle);          // 0:off 1: on
void  HID_Set_LED(u_int8  num, u_int8 mode,u_int16 toggle);
void  HID_GPIO_write (u_int8 num, u_int8 value);
u_int8  HID_GPIO_read (u_int8 num);


void HID_Send_UART(const u_int8 *ptr_fifo, u_int32 length);
void HIDMain_Handle_Message(u_int16 msg_id, void* msg_data);

void HID_Set_KB_Lock_Char_LED(u_int8 flag);
void HID_Connect_Complete(u_int8 result,u_int16 handle);
void HID_Disconnect_Complete(u_int8 result);
u_int8 HID_Schedule(void);
void HID_RoleChange(u_int8 result);
void HID_FLASH_LED(u_int8  num,u_int8 quickFlash);
void HID_Charge_INT_Handle(void);
void HID_Init_Spp(void);
void HID_Set_Sniff(void *arg);

void ADS129X_Init(void);
void ADS129X_TxCmd(u_int8 cmd1,u_int8 cmd2);
void ADS129X_WrReg(u_int8 addr, u_int8 num,u_int8 *ptr);
void ADS129X_RdReg(u_int8 addr,u_int8 num,u_int8 *ptr);

void HID_SPP_L2Connect_Complete(void);
void HID_L2cap_Connect_Complete(u_int16 handle);
void HID_LM_Connect_Complete(u_int8 result,u_int16 handle);
//void HID_rx_ack(u_int16 handle);
void HID_Run_Spec_Fun(u_int8 *data,u_int8 length);

void HID_Test_KB(u_int8 KeyCode);
void HID_GPIO_ISR(void);
void HID_PIN_SM(void);
void HID_Dispatch_Event(u_int8 event_code,t_lm_event_info* p_event_info);
void HID_Handle_TIM2(void);
struct q_desc* HID_Get_Link_Tx_Activity();


u_int8 SPI_Comm(u_int8 data,u_int8 isRd);





#endif     // ZGW_HID_HAL_H